/* File makes Table 4 */

/* 	Input files: fw_trade_add_on, deliverable_transactions.csv, 
	Output files: T4_Follow_xtrades.tex  */


**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************


use fw_trade_add_on, clear  

foreach xx of varlist _all {
	rename `xx' `=lower("`xx'")'   // "
}
keep messageid date  
gduplicates drop messageid, force
compress
save msg_dates, replace



clear
import delimited deliverable_transactions.csv 

gen byte bull_sentiment=cond(sentiment=="Bullish",1,0)
ren expected_trans pr_trade

drop sentiment transaction_code score
ren message_id messageid
ren user_id userid
ren symbol_id symbolid


merge 1:1 messageid using msg_dates, nogen keep(matched) 

bys userid symbolid date (pr_trade): keep if _n==_N 
gduplicates report userid symbolid date

save trade_messages, replace
keep if inrange(pr_trade,0.5,1) 

merge 1:1 date userid symbolid using temp_follows, keep(match using) nogen 

drop if (bull_sentiment==1 & bull==0) | (bull_sentiment==0 & bull==1) 
gen byte buy=cond(bull_sentiment==1,1,0)
gen byte sell=cond(bull_sentiment==0,1,0)

keep date userid symbolid bull bear buy sell morebull_t1t5 morebear_t1t5 
save temp_for_trading, replace


use temp_for_trading, clear


local lhsvar "morebull_t1t5"

eststo tradebull_base: reghdfe `lhsvar' i.bull , absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100
	cap estadd local UserFe "Y"
	cap estadd local UserSYMBFe "-"

eststo tradebull: reghdfe `lhsvar' i.bull i.bull#i.buy, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bull]/avg)*100
	cap estadd local UserFe "Y"	
	cap estadd local UserSYMBFe "-"

		eststo tradebull_baseFE: reghdfe `lhsvar' i.bull , absorb(i.userid#i.symbolid i.symbolid#i.date) vce(cl userid)
			qui sum `lhsvar' if e(sample)==1
			qui estadd scalar avg = r(mean)
			scalar define avg= r(mean)
			qui estadd scalar effect_size = (_b[1.bull]/avg)*100
			cap estadd local UserFe "-"
			cap estadd local UserSYMBFe "Y"
		
		local lhsvar "morebull_t1t5"
		eststo tradebullFE: reghdfe `lhsvar' i.bull i.bull#i.buy, absorb(i.userid#i.symbolid i.symbolid#i.date) vce(cl userid)
			qui sum `lhsvar' if e(sample)==1
			qui estadd scalar avg = r(mean)
			scalar define avg= r(mean)
			qui estadd scalar effect_size = (_b[1.bull]/avg)*100
			cap estadd local UserFe "-"
			cap estadd local UserSYMBFe "Y"
	
	
	
local lhsvar "morebear_t1t5"

eststo tradebear_base: reghdfe `lhsvar' i.bear , absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bear]/avg)*100
	cap estadd local UserFe "Y"
	cap estadd local UserSYMBFe "-"

eststo tradebear: reghdfe `lhsvar' i.bear i.bear#i.sell, absorb(i.userid i.symbolid#i.date) vce(cl userid)
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[1.bear]/avg)*100
	cap estadd local UserFe "Y"
	cap estadd local UserSYMBFe "-"
	
		eststo tradebear_baseFE: reghdfe `lhsvar' i.bear , absorb(i.userid#i.symbolid i.symbolid#i.date) vce(cl userid) 
			qui sum `lhsvar' if e(sample)==1
			qui estadd scalar avg = r(mean)
			scalar define avg= r(mean)
			qui estadd scalar effect_size = (_b[1.bear]/avg)*100
			cap estadd local UserFe "-"
			cap estadd local UserSYMBFe "Y"

		eststo tradebearFE: reghdfe `lhsvar' i.bear i.bear#i.sell, absorb(i.userid#i.symbolid i.symbolid#i.date) vce(cl userid) 
			qui sum `lhsvar' if e(sample)==1
			qui estadd scalar avg = r(mean)
			scalar define avg= r(mean)
			qui estadd scalar effect_size = (_b[1.bear]/avg)*100
			cap estadd local UserFe "-"
			cap estadd local UserSYMBFe "Y"

*****************************************************************************************************

cap estadd local dsymbolFe "Y" : *

#delimit ; 
local stats_opts " stats(N N_clust r2 avg effect_size UserFe UserSYMBFe dsymbolFe, fmt(%18.0fc %18.0fc %12.2fc %12.2fc %12.0fc %12.0fc) labels("\# observations" "\# clusters (users)" "$\textit{R}^2$" "Mean of dependent var.($\%$)" "Main effect size ($\%$ of mean)" "User FE" "User x symbol FE" "Day x symbol FE") ) " ;

local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles(
"Baseline"
"\shortstack{$+$ Buyer \\ interaction}"
"\shortstack{Baseline $+$ \\ UserSym FE}"
"\shortstack{$+$ Buyer \\ interaction}"

"Baseline"     
"\shortstack{$+$ Seller \\ interaction}"
"\shortstack{Baseline $+$ \\ UserSym FE}"
"\shortstack{$+$ Seller \\ interaction}"               )

mgroups("Dep. var.: $\mathbbm{1}$ x100 if net new Bull follows\textsubscript{i,s,(t+1$\rightarrow$ t+5)} >0" 
"Dep. var.: $\mathbbm{1}$ x100 if net new Bear follows\textsubscript{i,s,(t+1$\rightarrow$ t+5)} >0" 
, pattern(1 0 0 0 1 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

local filename "T4_Follow_xtrades" ;

cd "`=tabledir'" ;
esttab tradebull_base tradebull tradebull_baseFE tradebullFE tradebear_base tradebear tradebear_baseFE tradebearFE using `filename'.tex, `opts' `stats_opts' 
keep(1.bull 1.bull#1.buy 1.bear 1.bear#1.sell ) 
order (1.bull 1.bull#1.buy 1.bear)

coef(1.bull 	"$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t}" 
1.bear 			"$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t}"
1.bull#1.buy 	"$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} x $\mathbbm{1}$ Buyer\textsubscript{i,s,t}" 
1.bear#1.sell 	"$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t} x $\mathbbm{1}$ Seller\textsubscript{i,s,t}"  ) replace  ;
#delimit cr 
cd "`=maindir'" 


cap n erase temp_for_trading.dta




